package com.googlecode.monitoring.jobs;

import com.googlecode.monitoring.config.SpringApplicationContext;
import com.googlecode.monitoring.dao.AgentStatusDao;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.service.StateOfItem;
import org.apache.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * Author: Sergiy Doroshenko
 * Date: Jun 15, 2010
 * Time: 11:33:41 AM
 */
public class MonitoringRunJob extends QuartzJobBean {
    private Logger log = Logger.getLogger(MonitoringRunJob.class);
    private AgentStatusDao agentStatusDao;

    public MonitoringRunJob() {
        agentStatusDao = SpringApplicationContext.getBean(AgentStatusDao.class);
    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        ItemToCheck itemToCheck = (ItemToCheck) dataMap.get("item");
        ItemToCheck itemFromReport = agentStatusDao.getItemFromCheckReport(itemToCheck);
        StateOfItem stateOfItem = (StateOfItem)SpringApplicationContext.getBean(itemFromReport.getCurrentStateName().getClassName());
        stateOfItem.processItem(itemFromReport);
        agentStatusDao.saveItemToCheckReport(itemToCheck);
    }
}
